﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommonShop.Service;
using CommonShop.Service.Helper;
using CommonShop.Web.Areas.Manage.Models;
using NHibernate;

namespace CommonShop.Web.Areas.Manage.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Manage/Admin/

        public ActionResult Index()
        {
            if (Session["UserName"] != null) {
                return View();
            }
            else {
                return RedirectToAction("Login", "Admin");
            }
        }

        //
        // GET: /Main_Index/

        public ActionResult Main_Index() {
            if (Session["UserName"] == null) {
                return RedirectToAction("Index", "User");
            }
            else {
                string name = Session["UserName"].ToString();
                //NHibernateHelper helper = new NHibernateHelper();
                //ISession session = helper.GetSession();
                var factory = FluentSessionFactory.GetCurrentFactory();
                using (var session = factory.OpenSession()) {
                    IList<User> userList = session.CreateQuery(string.Format(@"from User where UserName='{0}' ", name)).List<User>();
                    if (userList.Count > 0) {
                        if (userList[0].Role.RoleID == Constants.Admin) {
                            ViewBag.Name = userList[0].UserName;
                            return View();
                        }
                        else {
                            return RedirectToAction("Index", "User");
                        }
                    }
                    else {
                        return RedirectToAction("Index", "User");
                    }
                }
            }

        }

        public ActionResult Login() {
            return View();
        }

        [HttpPost]
        public ActionResult Login(AdminLoginModel model) {
            if (ModelState.IsValid) {
                //NHibernateHelper helper = new NHibernateHelper();
                //ISessionFactory sessionFac = NHibernateHelper.CreateSessionFactory();
                //using (ISession session = sessionFac.OpenSession()) {
                //    IList<User> userList = session.CreateQuery(string.Format(@"from User where UserName='{0}' and UserPassword = '{1}'", model.UserName, model.Password)).List<User>();

                //    if (userList.Count > 0) {
                //        Session["UserName"] = userList[0].UserName;
                //        if (userList[0].Role.RoleID == Constants.Admin) {
                //            return RedirectToAction("Main_Index", "Admin");
                //        }
                //        return RedirectToAction("Index");
                //    }
                //    else {
                //        ViewBag.ErrorMsg = "登陆失败";
                //    }
                //    return View();
                //}
                var factory = FluentSessionFactory.GetCurrentFactory();
                using (var session = factory.OpenSession()) {
                    IList<User> userList = session.CreateQuery(string.Format(@"from User where UserName='{0}' and UserPassword = '{1}'", model.UserName, model.Password)).List<User>();

                    if (userList.Count > 0) {
                        Session["UserName"] = userList[0].UserName;
                        if (userList[0].Role.RoleID == Constants.Admin) {
                            return RedirectToAction("Main_Index", "Admin");
                        }
                        return RedirectToAction("Index");
                    }
                    else {
                        ViewBag.ErrorMsg = "登陆失败";
                    }
                    return View();
                }
            }
            return View(model);
        }


    }
}
